﻿using IntelligentStorWpfApp.Controller;
using IntelligentStorWpfApp.Model;
using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// AddCabinet.xaml 的交互逻辑
    /// </summary>
    public partial class AddCabinet : Window
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(AddCabinet));
        public AddCabinet()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
        private PositionController pc;
        private void AddCabinet_Loaded(object sender, RoutedEventArgs e)
        {
            pc = new PositionController();
            txtPosition.Text = "16";

        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            int cabinetNum, positionNum;
            try
            {

                if (int.TryParse(txtCabinet.Text, out cabinetNum) == false)
                {
                    txtValidate.Text = "柜号必须为数字!";
                    return;
                }
                if (int.TryParse(txtPosition.Text, out positionNum) == false)
                {
                    txtValidate.Text = "位置数量必须为数字!";
                    return;
                }



                //List<>
                for (int i = 1; i <= positionNum; i++)
                {
                    Position p = new Position();

                    p.CabinetNum = cabinetNum;
                    p.PositionNum = i;
                    int id = pc.getPostionId(p);
                    if (id > -1)
                    {

                    }
                    else
                    {
                        pc.addPosition(p);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            MessageBox.Show("新增箱柜" + cabinetNum.ToString() + "成功!");
            this.Close();
            
               // MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void txtCabinet_LostFocus(object sender, RoutedEventArgs e)
        {
            int cabinetNum;
            if (int.TryParse(txtCabinet.Text, out cabinetNum) == false)
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
                       new Action(() =>
                       {
                           txtValidate.Text = "柜号必须为数字!";
                           txtCabinet.Focus();
                       }));
            }
            else
            {
                List<int> listcabinets = new List<int>();
                DataTable dt = pc.getCabinetNums();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listcabinets.Add(Convert.ToInt32(dt.Rows[i][0]));
                }

                if(listcabinets.Contains(cabinetNum))
                {
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
                       new Action(() =>
                       {
                           txtValidate.Text="柜号:" + cabinetNum.ToString() + "已存在!";
                           txtCabinet.Focus();
                       }));
                }
            }
        }
    }
}
